/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/wifi-helper.h"
#include "ns3/yans-wifi-channel.h"
#include "ns3/mobility-helper.h"
#include "ns3/aodv-helper.h"
#include  "ns3/on-off-helper.h"
#include "ns3/rectangle.h"
#include "ns3/random-variable-stream.h"
#include "ns3/nqos-wifi-mac-helper.h"
using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int
main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);

Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

  NodeContainer wifiAdhocNodes;
wifiAdhocNodes.Create(16); //创建16无线自组网节点
WifiHelper wifi = WifiHelper::Default();
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default();//配置Mac与Phy
wifiMac.SetType("ns3::AdhocWifiMac");
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default(); //创建无线节点互连通道
wifiPhy.SetChannel(wifiChannel.Create());
NetDeviceContainer wifiAdhocDevices = wifi.Install(wifiPhy,wifiMac,wifiAdhocNodes);
InternetStackHelper stack;//安装协议栈
stack.Install(wifiAdhocNodes);
Ipv4AddressHelper address; //设置无线节点的IP地址
address.SetBase("10.1.1.0","255.255.255.0");
Ipv4InterfaceContainer AdhocInterfaces;
AdhocInterfaces = address.Assign(wifiAdhocDevices);

UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(wifiAdhocNodes.Get(15)); //服务端程序安装在节点n15｡
serverApps.Start(Seconds (1.0)); //服务端程序在1秒生效
serverApps.Stop(Seconds(10.0)); //服务端程序在10秒停止｡
UdpEchoClientHelper echoClient(AdhocInterfaces.GetAddress (15), 9);//让客户端与服务端及其端口关联
echoClient.SetAttribute ("MaxPackets", UintegerValue(100)); //设置通信时参数
echoClient.SetAttribute ("Interval", TimeValue (Seconds(1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (2048));
ApplicationContainer clientApps =echoClient.Install (wifiAdhocNodes.Get(0)); //客户端程序安装在节点n0
clientApps.Start (Seconds(1.0));//客户端在 2秒中生效
clientApps.Stop (Seconds(10.0)); //客户端在10秒钟停止

Simulator::Run();

Simulator::Destroy();
return 0;
}
